<?php

// Copyright 2022 Hackware SpA <human@hackware.cl>
// "Hackware Web Services Core" is released under the MIT License terms.

namespace Hawese\Core\Support;

// Optionally case insensitive DRY array functions.
class Arr
{
    public static function keyExists(
        array $array,
        string $key,
        bool $caseInsensitive = true
    ): bool {
        if ($caseInsensitive) {
            $array = array_change_key_case($array);
            $key = strtolower($key);
        }
        if (array_key_exists($key, $array)) {
            return true;
        }
        return false;
    }

    public static function get(
        array $array,
        string $key,
        bool $caseInsensitive = true
    ) {
        if ($caseInsensitive) {
            $array = array_change_key_case($array);
            $key = strtolower($key);
        }
        if (array_key_exists($key, $array)) {
            return $array[$key];
        }
        return null;
    }
}
